package net.maku.generator.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 本地资源映射配置
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Configuration
@ConditionalOnProperty(prefix = "storage", value = "enabled")
public class UploadResourceConfiguration implements WebMvcConfigurer {

    /**
     * 本地存储路径
     */
    @Value("${storage.enabled}")
    private boolean storageEnabled;

    /**
     * 资源起始路径
     */
    @Value("${storage.upload-url}")
    private String localUrl;

    /**
     * 本地存储路径
     */
    @Value("${storage.upload-dir}")
    private String storageLocalPath;

    /**
     * 本地存储路径
     */
    @Value("${storage.template-dir}")
    private String storageTemplatePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 如果不是本地存储，则返回
        if (!storageEnabled) {
            return;
        }

        registry.addResourceHandler("/" + localUrl + "/**")
                .addResourceLocations("file:" + storageLocalPath);
    }
}